package com.softer.chengxin.score.dao;

import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.score.dao.provider.ScoreDetailDaoProvider;
import com.softer.chengxin.score.entity.Appeal;
import com.softer.chengxin.score.entity.ScoreDTO;
import com.softer.chengxin.score.entity.ScoreDetail;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2018/9/26
 */
@Mapper
@Repository
public interface ScoreDetailDao {

    /**
     * 保存信息录入
     *
     * @param scoreDetail 信息实体
     * @param id          企业ID
     * @return 处理结果
     */
    @Insert("INSERT INTO score_detail(corp_id,project_id,adder,reason,state,bjdw,bjsj,file_path_id,gmt_create,gmt_modified,info_type,remark,ssfj_id)" +
            " VALUES(#{id},#{score.projectId},#{score.adder},#{score.reason},#{score.state},#{score.department},#{score.time},#{score.fileId},now(),now(),#{score.infoType},#{score.remark},#{score.fileId})")
    int addScoreDetail(@Param("score") ScoreDetail scoreDetail, @Param("id") Integer id);

    /**
     * 先保存后，提交信息录入，只需要修改状态
     *
     * @param id    得分详情ID
     * @param state 状态
     * @return 处理结果
     */
    @Update("UPDATE score_detail SET state=#{state} WHERE id=#{id}")
    int updateState(@Param("id") Integer id, @Param("state") Integer state);

    /**
     * 获取录入信息详情
     *
     * @param id 录入信息Id
     * @return 信息详情
     */
    @Select("SELECT a.id,a.score_type_id AS scoreTypeId,(SELECT score_name FROM score_type WHERE id = a.score_type_id) AS scoreTypeName,a.score,DATE_FORMAT(a.effective_begin_time, '%Y-%m-%d') as effectiveBeginTime,DATE_FORMAT(a.effective_end_time, '%Y-%m-%d') as effectiveEndTime,a.reason,a.remark,a.file_path_id as fileId,a.ssnr,a.ssfj_id as ssfjId," +
            "DATE_FORMAT(a.publicity_begin_time, '%Y-%m-%d') as publicityBeginTime,DATE_FORMAT(a.publicity_end_time, '%Y-%m-%d') as publicityEndTime,a.state ,m.name as approver,a.project_id as projectId,DATE_FORMAT(a.bjsj, '%Y-%m-%d') as date,a.bjdw as department,a.shyj," +
            "b.project_name as projectName,a.zg_bgyj as zgBgyj,a.score_type_detail_id as scoreTypeDetailId FROM score_detail a LEFT JOIN company_project b ON b.id =a.project_id left join manager_user m on m.login_name=a.approver WHERE a.id=#{id}")
    ScoreDetail getScoreDetail(@Param("id") Integer id);

    /**
     * 删除录入信息
     *
     * @param id 录入信息ID
     * @return 处理结果
     */
    @Delete("DELETE FROM score_detail WHERE id=#{id}")
    int delete(@Param("id") Integer id);

    /**
     * 查询录入信息数量
     *
     * @param scoreDTO  查询实体
     * @param states    不同的页面查询不同的状态信息
     * @param loginName 登陆名
     * @return 数量
     */
    @SelectProvider(type = ScoreDetailDaoProvider.class, method = "queryNum")
    int queryNum(@Param("scoreDTO") ScoreDTO scoreDTO, @Param("states") String states, @Param("loginName") String loginName);

    /**
     * 查询录入信息列表
     *
     * @param page      页面实体
     * @param scoreDTO  查询实体
     * @param states    不同的页面查询不同的状态信息
     * @param loginName 登陆名
     * @return 列表
     */
    @SelectProvider(type = ScoreDetailDaoProvider.class, method = "queryList")
    List<ScoreDetail> queryList(@Param("page") Page page, @Param("scoreDTO") ScoreDTO scoreDTO, @Param("states") String states, @Param("loginName") String loginName);

    /**
     * 企业申诉
     *
     * @param appeal 申诉实体
     * @return 结果
     */
    @Update("UPDATE score_detail SET ssnr=#{appeal.appealContent},state=4,ssfj_id=#{appeal.appealFileId},sssj=NOW() WHERE id=#{appeal.id}")
    int appeal(@Param("appeal") Appeal appeal);

    /**
     * 获取最近的信息（用于首页展示）
     *
     * @param id        企业ID
     * @param projectId 项目ID
     * @return 信息集合
     */
    @SelectProvider(type = ScoreDetailDaoProvider.class, method = "getCurrentMsg")
    List<ScoreDetail> getCurrentMsg(@Param("id") Integer id, @Param(Common.PROJECT_ID) String projectId);

    /**
     * 编辑录入信息
     *
     * @param scoreDetail 录入信息实体
     * @return 录入结果
     */
    @Update("UPDATE score_detail SET state=0, project_id=#{s.projectId},reason=#{s.reason},bjdw=#{s.department},bjsj=#{s.time},remark=#{s.remark},file_path_id=#{s.fileId},gmt_modified=NOW(),flag=0 WHERE id=#{s.id}")
    int edit(@Param("s") ScoreDetail scoreDetail);

    /**
     * 查询数据库中的年度集合
     *
     * @return 年度集合
     */
    @Select("SELECT year FROM score_composite GROUP BY year")
    List<String> getYearList();

    /**
     * 查询分数类型的使用次数
     *
     * @param id 分数类型ID
     * @return 分数类型的数量
     */
    @Select("SELECT COUNT(1) FROM score_detail WHERE score_type_detail_id=#{id}")
    int getScoreTypeDetailNum(@Param("id") int id);
}
